//
//  FirstViewController.m
//  UI12_homework_2
//
//  Created by dllo on 15/11/10.
//  Copyright (c) 2015年 ZKJ. All rights reserved.
//

#import "FirstViewController.h"
#import "FirstModel.h"
#import "QQTableViewCell.h"


@interface FirstViewController ()<UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, retain) UITableView *tableView;
@property (nonatomic, retain) FirstModel *firstModel;

@end

@implementation FirstViewController

- (void)dealloc {
    [_tableView release];
    [super dealloc];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.view.backgroundColor = [UIColor redColor];
//    self.title = @"第一页";
    
    self.navigationController.navigationBar.translucent = NO;
    UISegmentedControl *seg = [[UISegmentedControl alloc] initWithItems:@[@"消息", @"电话"]];
    seg.frame = CGRectMake(0, 0, 100, 30);
    seg.tintColor = [UIColor whiteColor];
    self.navigationItem.titleView = seg;
    seg.selectedSegmentIndex = 0;
    [seg release];
    
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[[UIImage imageNamed:@"touxiang@2x.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] style:UIBarButtonItemStyleDone target:self action:@selector(leftAction:)];

    
//    UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
//    button.frame = CGRectMake(20, 20, 60, 60);
//    [button setBackgroundImage:[UIImage imageNamed:@"touxiang@2x.png"] forState:UIControlStateNormal];
//    [self.view addSubview:button];
    
    self.navigationController.navigationBar.barTintColor = [UIColor colorWithRed:20 / 255.0 green:175 / 255.0 blue:231 / 255.0 alpha:1];
    
//    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemReply target:self action:@selector(rightAction:)];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"menu@2x.png"] style:UIBarButtonItemStyleDone target:self action:@selector(rightAction:)];
    
    
    
    
    NSDictionary *dic = [[NSDictionary alloc] initWithContentsOfFile:@"/Users/dllo/Desktop/UI12_homework_2/UI12_homework_2/YiChe.plist"];
    NSDictionary *dic2 = [[NSDictionary alloc] initWithDictionary:dic[@"data"]];
    NSLog(@"%@", dic2);
    _firstModel = [[FirstModel alloc] init];
    [_firstModel setValuesForKeysWithDictionary:dic2];
    
    
    [self.view addSubview:self.tableView];
    [self.tableView release];
    
    
    UISearchBar *searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 50)];
    searchBar.placeholder = @"查找联系人";
    searchBar.tag = 200;
    [self.view addSubview:searchBar];
    [searchBar release];
    
}

- (void)leftAction:(UIBarButtonItem *)sender {
    
}

- (void)rightAction:(UIBarButtonItem *)sender {
    
}

#pragma mark - UITableViewDataSource

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _firstModel.dataSource.count;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    UISearchBar *search = (UISearchBar *)[self.view viewWithTag:200];
    [search resignFirstResponder];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    QQTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"UITableViewCellIdentifier"];
    if (_firstModel.dataSource.count > indexPath.row) {
        [cell setSecondModel:_firstModel.dataSource[indexPath.row]];
    }
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 60;
}

- (UITableView *)tableView {
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 50, self.view.frame.size.width, self.view.frame.size.height - 50) style:UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        [_tableView registerClass:[QQTableViewCell class] forCellReuseIdentifier:@"UITableViewCellIdentifier"];
    }
    return _tableView;
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
